package processCode;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * Description
 *
 * @ClassName processCode
 * @Copyright 炫彩互动
 * @Author yanglh
 * @Create Date  2016/4/28
 * @Modified by none
 * @Modified Date
 */

public class ConvertCode {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "你好";

// 编码
        byte[] utf = s.getBytes("utf-8");
        byte[] gbk = s.getBytes("gbk");

        System.out.println("utf-8编码：" + Arrays.toString(utf)); // [-28, -67, -96, -27, -91, -67]  6个字节
        System.out.println("gbk编码：" + Arrays.toString(gbk)); // [-60, -29, -70, -61]<span style="white-space:pre">	</span>4个字节

// 解码
        String s1 = new String(utf, "utf-8"); // 你好
        String s2 = new String(utf, "gbk");// gbk解码：浣犲ソ gbk用2个字节解码，所以会多一个字符
        String s3 = new String(gbk, "utf-8");// gbk用utf-8解码：??? <span style="white-space:pre">	</span>utf-8解码需要6个字节
        String s4 = new String(utf, "gbk");// gbk解码：浣犲ソ gbk用2个字节解码，所以会多一个字符
        System.out.println("--------------------");
        System.out.println("utf-8解码：" + s1);
        System.out.println("utf-8用gbk解码：" + s2);
        System.out.println("gbk用utf-8解码：" + s3);
        System.out.println("---------------------");

        System.out.println("utf-8用gbk解码后可以编回去");
        s2 = new String(s2.getBytes("gbk"), "utf-8");  // 锟斤拷锟?   gbk用utf-8解码后无法编回去
        System.out.println(s2);

        System.out.println("---------------------");
        System.out.println("gbk用utf-8解码后无法编回去");
        s3 = new String(s3.getBytes("utf-8"), "gbk");  // 锟斤拷锟?   gbk用utf-8解码后无法编回去
        System.out.println(s3);
    }
}
